package com.wanmait.gohome.controller.web;

import com.github.pagehelper.PageInfo;
import com.wanmait.gohome.pojo.Journal;
import com.wanmait.gohome.pojo.Task;
import com.wanmait.gohome.pojo.Userinfo;
import com.wanmait.gohome.service.JournalService;
import com.wanmait.gohome.util.CookieUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/web/blogger")
public class JournalController {

    @Resource
    private JournalService journalService;

    @GetMapping("fabu")//跳到发布日志界面
    public void  fabu()
    {

    }

    @GetMapping("blog")//跳转到日志jsp界面
    public ModelAndView  blog()
    {
        ModelAndView mv = new ModelAndView();
        List<Journal> journals = journalService.findTodayHot(5);
        mv.addObject("journals",journals);
        return mv;
    }
    @RequestMapping("bloggers")//跳转到blog.jsp初始化
    @ResponseBody
    public ModelAndView bloggers()
    {
//      Userinfo userinfo = (Userinfo) request.getSession().getAttribute("userinfo");
        PageInfo<Journal> pageInfo = journalService.findPage();
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo", pageInfo);
        return mv;
    }
    @RequestMapping("souSuo")//条件搜索solr
    @ResponseBody
    public ModelAndView souSuo(String title)
    {
        PageInfo<Journal> pageInfo = journalService.findByTitle(title);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo", pageInfo);
        return mv;
    }
    @RequestMapping("addLike")//点赞添加以及不能重复点赞
    @ResponseBody
    public String addLike(Integer id, HttpServletResponse response, HttpServletRequest request) {
        // |1|2|3|
       String like = CookieUtil.getCookie("like",request);
       String result;
        if(like==null)
        {
            like = "|"+id+"|";
            result="ok";
        }
        else
        {
            if(like.contains("|"+id+"|"))
            {
                result="fail";
            }
            else
            {
                like += id+"|";
                result="ok";
            }
        }
        if(result.equals("ok")) {
            journalService.insertLike(id);
            CookieUtil.addCookie("like", like, "/", 60 * 60 * 24 * 30, response);//把日志id存到cook里面
        }
        return result;
    }
    @RequestMapping("blogss")//日志浏览量加一
    @ResponseBody
    public void blogss(String idStrs)
    {
        int ids = Integer.parseInt(idStrs);
        journalService.addById(ids);
    }
    @RequestMapping("blogs")//日志点击标题跳转到blogs详情页页面
    public ModelAndView blogs(String id)
    {
        int is = Integer.parseInt(id);
        Journal journal = journalService.findByIds(is);
        List<Journal> journals = journalService.findTodayHot(5);
        journalService.addById(is);
        ModelAndView mv = new ModelAndView();
        mv.addObject("journals",journals);
        mv.addObject("journal", journal);
        return mv;
    }

    @PostMapping("addlog")
    public String addlog(HttpServletRequest request,Journal journal, MultipartFile pic1) //发布日志
    {

        String ext = FilenameUtils.getExtension(pic1.getOriginalFilename());
        String fileName = UUID.randomUUID() + "." + ext;
        String path = request.getServletContext().getRealPath("web/userinfo/img/journalImages/") + fileName;
        File outFile = new File(path);
        try {
            pic1.transferTo(outFile);
        } catch (Exception e) {
        }
        if(ext!=""){
            journal.setPics(fileName);
        }
        journalService.sendLogs(journal);
        return "redirect:/web/blogger/blog";
    }


    /**
     *
     * @param request
     * @param journal 个人中心 修改日志
     * @param pic1
     * @return
     */
    @PostMapping("updateByJournalId")
    public String updateByJournalId(HttpServletRequest request,Journal journal,MultipartFile pic1)
    {
        String ext = FilenameUtils.getExtension(pic1.getOriginalFilename());
        String fileName = UUID.randomUUID() + "." + ext;
        String path = request.getServletContext().getRealPath("web/userinfo/img/journalImages/") + fileName;
        File outFile = new File(path);
        try {
            pic1.transferTo(outFile);
        } catch (Exception e) {
        }
        if(ext!=""){
            journal.setPics(fileName);
        }
        journalService.updateByJournalId(journal);
        return "redirect:/web/blogger/blog";
    }

}
